/**
 * Created by zilong on 8/4/15.
 */
var gulp =require('gulp')
var livereload = require('gulp-livereload');
var plumber =require('gulp-plumber')
var less = require('gulp-less')
var glob = require('glob');
var path = require('path')
var minifyCSS = require('gulp-minify-css');
var sourcemaps = require('gulp-sourcemaps');



gulp.task('watch',function(){
    livereload.listen();
    return gulp.watch('./public/less/**/*.less',['less4watch'])
})

gulp.task('less4watch',function(){
    var lessPath = './public/less'
    var files = glob.sync('./public/less/**/*_build.less');
    var dirs = [lessPath];
    for(var i in files){
        dirs.push(path.dirname(files[i]))
    }

    return gulp.src(files,{base:lessPath})
        .pipe(plumber())
        .pipe(less({
            paths:dirs
        }))
        .pipe(gulp.dest('./public/build_css'))
        .pipe(livereload())
})


gulp.task('less',function(){
    var lessPath = './public/less'
    var files = glob.sync('./public/less/**/*_build.less');
    var dirs = [lessPath];
    for(var i in files){
        dirs.push(path.dirname(files[i]))
    }

    return gulp.src(files,{base:lessPath})
        .pipe(plumber())
        .pipe(less({
            paths:dirs
        }))
        .pipe(minifyCSS())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./public/build_css'))
})

gulp.task('default',['less'])